import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *实现在 博客列表页,博客详情页 中显示用户信息 的后端代码.
 */
@WebServlet("/userInfo")
public class UserInfoServlet extends HttpServlet {

    private final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取用户信息.

        String blogId = req.getParameter("blogId");
        if (blogId == null) {
            // 博客列表页, 获取当前登录的用户信息.
            // 直接从 session 获取即可.
            getUserInfoFromSession(req,resp);
        } else {
            // 博客详情页, 获取当前博客作者的相关信息.
            // 查询数据库获取.
            getUserInfoFromDB(req,resp,Integer.parseInt(blogId));
        }
    }

    private void getUserInfoFromDB(HttpServletRequest req, HttpServletResponse resp, int blogId) throws IOException {
        // 1. 先根据 blogId 查询到 blog 对象, 获取到 userId (作者是谁).
        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.selectOne(blogId);
        if(blog == null) {
            // 如果传来的 blogId 参数是瞎写的, 数据库里不存在.
            resp.setStatus(404); // 404表示资源不存在.
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("访问的资源不存在!");
            return;
        }
        // 2. 根据 userId 查询对应的 User 对象.
        UserDao userDao = new UserDao();
        User user = userDao.selectById(blog.getUserId());
        if (user == null) {
            resp.setStatus(404); // 404表示资源不存在.
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("访问的资源不存在!");
            return;
        }
        // 3. 将 User 对象返回给浏览器.
        user.setPassword(""); // 将密码置为空.
        resp.setContentType("application/json; charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));
    }

    private void getUserInfoFromSession(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setStatus(403);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前未登录!");
            // resp.setStatus(302);
            // resp.sendRedirect("error.html");
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.setStatus(403);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("当前未登录!");
            // resp.setStatus(302);
            // resp.sendRedirect("error.html");
            return;
        }
        // user 获取到, 把 user 里的 password 干掉, 然后返回.
        user.setPassword("");
        resp.setContentType("application/json; charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));
    }
}
